#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QInputDialog"

#include <QPainter>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->splitter->setStretchFactor(1, 1);
    ui->treeWidget->clear();

    ui->treeWidget->setColumnCount(3);
    ui->treeWidget->setHeaderHidden(false);

    QTreeWidgetItem *header = new QTreeWidgetItem();
    header->setText(_colName, "名称");
    header->setTextAlignment(_colName, Qt::AlignVCenter | Qt::AlignLeft);
    header->setText(_colType, "类型");
    header->setTextAlignment(_colType, Qt::AlignVCenter | Qt::AlignLeft);
    header->setText(_colId, "Id");
    header->setTextAlignment(_colId, Qt::AlignVCenter | Qt::AlignLeft);
    ui->treeWidget->setHeaderItem(header);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::visit(QTreeWidgetItem *item, std::function<void(QTreeWidgetItem*)> fun)
{
    for(int i = 0; i < item->childCount(); ++ i){
        visit(item->child(i), fun);
    }
    fun(item);
}


void MainWindow::on_treeWidget_itemSelectionChanged()
{
    auto items = ui->treeWidget->selectedItems();
    ui->chartWidget->clearSelectCurve();
    for(auto item:items){
        visit(item, [&](QTreeWidgetItem* item){
            if(item->type() == _itCurveItem){
                ui->chartWidget->selectCurve(item->text(_colId));
            }
        });
    }

}

void MainWindow::on_actNewNode_triggered()
{
    QString title = "添加节点";
    QString label = "请输入节点名称";
    QString initInput = "节点";
    QLineEdit::EchoMode echoMode = QLineEdit::Normal;
    bool ok = false;
    QString text = QInputDialog::getText(this, title, label, echoMode, initInput, &ok);
    if(ok && !text.isEmpty()){
        auto items = ui->treeWidget->selectedItems();
        QTreeWidgetItem* item;
        if(items.size() > 0){
            QTreeWidgetItem* cur = items.front();
            for(int i = 0; i < cur->childCount(); ++ i){
                if(cur->child(i)->text(0) == text && cur->child(i)->type() == _itNodeItem){
                    return;
                }
            }
            item = new QTreeWidgetItem(_itNodeItem);
            cur->addChild(item);
        }else{
            auto items = ui->treeWidget->findItems(text, Qt::MatchFixedString, _colName);
            for(auto it:items){
                if(it->type() == _itNodeItem)
                    return;
            }
            item = new QTreeWidgetItem(_itNodeItem);
            ui->treeWidget->addTopLevelItem(item);
        }

        item->setText(_colName, text);
        QIcon icon(":/icons/type_node.png");
        item->setIcon(_colType, icon);
    }
}

void MainWindow::on_actNewCurve_triggered()
{
    QString title = "新建曲线";
    QString label = "请输入曲线名称";
    QString initInput = "曲线";
    QLineEdit::EchoMode echoMode = QLineEdit::Normal;
    bool ok = false;
    QString text = QInputDialog::getText(this, title, label, echoMode, initInput, &ok);
    if(ok && !text.isEmpty()){
        auto items = ui->treeWidget->selectedItems();
        QTreeWidgetItem* item;
        if(items.size() > 0){
            QTreeWidgetItem* cur = items.front();
            if(cur->type() == _itCurveItem)
                return;
            for(int i = 0; i < cur->childCount(); ++ i){
                if(cur->child(i)->text(0) == text && cur->child(i)->type() == _itCurveItem){
                    return;
                }
            }
            item = new QTreeWidgetItem(_itCurveItem);
            cur->addChild(item);
        }else{
            auto items = ui->treeWidget->findItems(text, Qt::MatchFixedString, _colName);
            for(auto it:items){
                if(it->type() == _itCurveItem)
                    return;
            }
            item = new QTreeWidgetItem(_itCurveItem);
            ui->treeWidget->addTopLevelItem(item);
        }
        item->setText(_colName, text);
        QString id = ui->chartWidget->addCurve(text);
        QColor color = ui->chartWidget->getCurveColor(id);
        QPixmap pixmap(":/icons/icons_curve.png");
        QPainter painter(&pixmap);
        painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
        painter.fillRect(pixmap.rect(), color);
        QIcon colorIcon = QIcon(pixmap);

        item->setIcon(_colType, colorIcon);
        item->setText(_colId, id);
    }
}


void MainWindow::on_actDelete_triggered()
{
    auto items = ui->treeWidget->selectedItems();
    for(auto item:items){
        visit(item, [&](QTreeWidgetItem* item){
            if(item->type() == _itCurveItem){
                ui->chartWidget->deleteCurve(item->text(_colId));
            }
            delete item;
        });
    }
}

